#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#define STACK_NUM 10


//先进后出
typedef struct Stack
{
	int arr[STACK_NUM];
	int bottom;   //栈底
	int top;//栈顶，新数据存放的位置
}Stack;
//入栈
void push(Stack *stack, int data)
{
	if (isFull(stack))
	{
		printf("入栈失败,栈满...\n");
		return;
	}
	stack->arr[stack->top] = data;
	stack->top++;
}

//出栈:将最后进去的数据扔掉
void pop(Stack *stack)
{
	if (isEmpty(stack))
	{
		printf("出栈失败,栈空...\n");
		return;
	}
	stack->top--;
	stack->arr[stack->top] = 0;
}

//获得栈顶元素的值
int getTop(Stack *stack)
{
	if (isEmpty(stack))
	{
		printf("获得栈顶元素失败,栈空...\n");
		return;
	}
	return stack->arr[stack->top-1];
}

//判断栈是否满了
int isFull(Stack *stack)
{	
	int ret = 0;
	if (NULL != stack)
	{
		if (STACK_NUM == stack->top)
		{
			ret = 1;
		}
	}
	return ret;
}

//判断栈是否空了
int isEmpty(Stack *stack)
{
	int ret = 0;
	if (NULL != stack)
	{
		if (stack->bottom+1 == stack->top)
		{
			ret = 1;
		}
	}
	return ret;
}

int main(void)
{
	Stack stack;
	memset(&stack, 0, sizeof(stack));
	stack.top = 0;
	stack.bottom = -1;

	int iNum = 0;
	while (1)
	{
		printf("请输入入栈数据:\n");
		scanf("%d", &iNum);
		push(&stack, iNum);
		if (isFull(&stack))
		{
			break;
		}
	}

	while (1)
	{
		if (isEmpty(&stack))
		{
			break;
		}
		printf("data=%d\n", getTop(&stack));
		pop(&stack);
	}

	return 0;
}
